package cn.edu.scau.cmi.hombio.web.rest;

import cn.edu.scau.cmi.hombio.dao.TaskDAO;
import cn.edu.scau.cmi.hombio.dao.TaskIntervalCycleDAO;

import cn.edu.scau.cmi.hombio.domain.Task;
import cn.edu.scau.cmi.hombio.domain.TaskIntervalCycle;

import cn.edu.scau.cmi.hombio.service.TaskIntervalCycleService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Spring Rest controller that handles CRUD requests for TaskIntervalCycle entities
 * 
 */

@Controller("TaskIntervalCycleRestController")

public class TaskIntervalCycleRestController {

	/**
	 * DAO injected by Spring that manages Task entities
	 * 
	 */
	@Autowired
	private TaskDAO taskDAO;

	/**
	 * DAO injected by Spring that manages TaskIntervalCycle entities
	 * 
	 */
	@Autowired
	private TaskIntervalCycleDAO taskIntervalCycleDAO;

	/**
	 * Service injected by Spring that provides CRUD operations for TaskIntervalCycle entities
	 * 
	 */
	@Autowired
	private TaskIntervalCycleService taskIntervalCycleService;

	/**
	 * Delete an existing TaskIntervalCycle entity
	 * 
	 */
	@RequestMapping(value = "/TaskIntervalCycle/{taskintervalcycle_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteTaskIntervalCycle(@PathVariable Integer taskintervalcycle_id) {
		TaskIntervalCycle taskintervalcycle = taskIntervalCycleDAO.findTaskIntervalCycleByPrimaryKey(taskintervalcycle_id);
		taskIntervalCycleService.deleteTaskIntervalCycle(taskintervalcycle);
	}

	/**
	* Register custom, context-specific property editors
	* 
	*/
	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
		binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
		binder.registerCustomEditor(byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());
		binder.registerCustomEditor(boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));
		binder.registerCustomEditor(Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));
		binder.registerCustomEditor(java.math.BigDecimal.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(java.math.BigDecimal.class, true));
		binder.registerCustomEditor(Integer.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));
		binder.registerCustomEditor(java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());
		binder.registerCustomEditor(String.class, new org.skyway.spring.util.databinding.StringEditor());
		binder.registerCustomEditor(Long.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Long.class, true));
		binder.registerCustomEditor(Double.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Double.class, true));
	}

	/**
	* View an existing Task entity
	* 
	*/
	@RequestMapping(value = "/TaskIntervalCycle/{taskintervalcycle_id}/task/{task_id}", method = RequestMethod.GET)
	@ResponseBody
	public Task loadTaskIntervalCycleTask(@PathVariable Integer taskintervalcycle_id, @PathVariable Integer related_task_id) {
		Task task = taskDAO.findTaskByPrimaryKey(related_task_id, -1, -1);

		return task;
	}

	/**
	* Select an existing TaskIntervalCycle entity
	* 
	*/
	@RequestMapping(value = "/TaskIntervalCycle/{taskintervalcycle_id}", method = RequestMethod.GET)
	@ResponseBody
	public TaskIntervalCycle loadTaskIntervalCycle(@PathVariable Integer taskintervalcycle_id) {
		return taskIntervalCycleDAO.findTaskIntervalCycleByPrimaryKey(taskintervalcycle_id);
	}

	/**
	* Save an existing Task entity
	* 
	*/
	@RequestMapping(value = "/TaskIntervalCycle/{taskintervalcycle_id}/task", method = RequestMethod.PUT)
	@ResponseBody
	public Task saveTaskIntervalCycleTask(@PathVariable Integer taskintervalcycle_id, @RequestBody Task task) {
		taskIntervalCycleService.saveTaskIntervalCycleTask(taskintervalcycle_id, task);
		return taskDAO.findTaskByPrimaryKey(task.getId());
	}

	/**
	* Create a new TaskIntervalCycle entity
	* 
	*/
	@RequestMapping(value = "/TaskIntervalCycle", method = RequestMethod.POST)
	@ResponseBody
	public TaskIntervalCycle newTaskIntervalCycle(@RequestBody TaskIntervalCycle taskintervalcycle) {
		taskIntervalCycleService.saveTaskIntervalCycle(taskintervalcycle);
		return taskIntervalCycleDAO.findTaskIntervalCycleByPrimaryKey(taskintervalcycle.getId());
	}

	/**
	* Save an existing TaskIntervalCycle entity
	* 
	*/
	@RequestMapping(value = "/TaskIntervalCycle", method = RequestMethod.PUT)
	@ResponseBody
	public TaskIntervalCycle saveTaskIntervalCycle(@RequestBody TaskIntervalCycle taskintervalcycle) {
		taskIntervalCycleService.saveTaskIntervalCycle(taskintervalcycle);
		return taskIntervalCycleDAO.findTaskIntervalCycleByPrimaryKey(taskintervalcycle.getId());
	}

	/**
	* Create a new Task entity
	* 
	*/
	@RequestMapping(value = "/TaskIntervalCycle/{taskintervalcycle_id}/task", method = RequestMethod.POST)
	@ResponseBody
	public Task newTaskIntervalCycleTask(@PathVariable Integer taskintervalcycle_id, @RequestBody Task task) {
		taskIntervalCycleService.saveTaskIntervalCycleTask(taskintervalcycle_id, task);
		return taskDAO.findTaskByPrimaryKey(task.getId());
	}

	/**
	* Get Task entity by TaskIntervalCycle
	* 
	*/
	@RequestMapping(value = "/TaskIntervalCycle/{taskintervalcycle_id}/task", method = RequestMethod.GET)
	@ResponseBody
	public Task getTaskIntervalCycleTask(@PathVariable Integer taskintervalcycle_id) {
		return taskIntervalCycleDAO.findTaskIntervalCycleByPrimaryKey(taskintervalcycle_id).getTask();
	}

	/**
	* Show all TaskIntervalCycle entities
	* 
	*/
	@RequestMapping(value = "/TaskIntervalCycle", method = RequestMethod.GET)
	@ResponseBody
	public List<TaskIntervalCycle> listTaskIntervalCycles() {
		return new java.util.ArrayList<TaskIntervalCycle>(taskIntervalCycleService.loadTaskIntervalCycles());
	}

	/**
	* Delete an existing Task entity
	* 
	*/
	@RequestMapping(value = "/TaskIntervalCycle/{taskintervalcycle_id}/task/{task_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteTaskIntervalCycleTask(@PathVariable Integer taskintervalcycle_id, @PathVariable Integer related_task_id) {
		taskIntervalCycleService.deleteTaskIntervalCycleTask(taskintervalcycle_id, related_task_id);
	}
}